package com.kehao.leetcode.jzoffer;

import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/14 9:56
 */
public class JZSolution014 {
    public boolean checkInclusion(String s1, String s2) {
        int s1Length = s1.length();
        if (s1Length > s2.length()) return false;
        int[] s1Array = new int[26];
        int[] subStrArray = new int[26];
        for (int i = 0; i < s1Length; i++) {
            char c = s1.charAt(i);
            s1Array[c - 'a']++;
            char c2 = s2.charAt(i);
            subStrArray[c2 - 'a']++;
        }
        for (int i = s1Length; i < s2.length(); i++) {
            boolean flag = true;
            for (int j = 0; j < s1Array.length; j++) {
                if (s1Array[j] != subStrArray[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag == true) {
                return flag;
            }
            char c1 = s2.charAt(i-s1Length);
            char c2 = s2.charAt(i);
            subStrArray[c1-'a']--;
            subStrArray[c2-'a']++;
        }
        boolean flag = true;
        for (int j = 0; j < s1Array.length; j++) {
            if (s1Array[j] != subStrArray[j]) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    @Test
    public void test01() {
        String s1 = "adc", s2 = "dcda";
        System.out.println(checkInclusion(s1, s2));
    }
}
